package com.tekj.cxqcb.view.Module.activity;

import android.content.DialogInterface;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.config.enums.EventBusMsgBean;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.PubStatusBean;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import common.tool.Tools;
import common.widget.GetBtnCodeView;
import es.dmoral.toasty.Toasty;

import static com.tekj.cxqcb.config.enums.EventBusMsgBean.EnumStatus.个人信息修改;


/**
 * 换绑手机号
 */
public class PhoneBindingActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.tv_old_phone)
    TextView tvOldPhone;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_verification)
    EditText etVerification;
    @BindView(R.id.tv_send_code)
    TextView tvSendCode;

    BusinessDao businessDao;

    @Override
    public void init() {
        tvTitle.setText("");
        businessDao = new BusinessDao(this, this);
    }

    @Override
    public int setLayout() {
        return R.layout.activity_phone_binding;
    }

    @Override
    public void initLoadStart() {

    }

    @Override
    public void initLoadEnd() {

    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 短信验证码:
                PubStatusBean bean = (PubStatusBean) bindingViewBean.getBean();
                if ("0000000".equals(bean.getCode())) {
                    GetBtnCodeView mGetBtnCodeView = new GetBtnCodeView(tvSendCode, 60000, 1000);
                    mGetBtnCodeView.start();
                    Toasty.success(this, bean.getMsg()).show();
                } else
                    Toasty.error(this, bean.getMsg()).show();
                break;

            case 修改手机号:
                PubStatusBean pubStatusBean = (PubStatusBean) bindingViewBean.getBean();
                if ("0000000".equals(pubStatusBean.getCode())) {
                    EventBus.getDefault().post(new EventBusMsgBean(个人信息修改, "手机号修改"));
                    finish();
                } else
                    Toasty.normal(this, pubStatusBean.getMsg()).show();

                break;
        }
    }


    @OnClick({R.id.ll_back, R.id.tv_send_code, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.tv_send_code:
                //TODO 验证码
                if (etPhone.getText().toString().trim().equals("")) {
                    Toasty.warning(this, "请先填写手机号").show();
                    return;
                }
                businessDao.SendCode(etPhone.getText().toString().trim());
                break;
            case R.id.tv_submit:
                //TODO 提交
                Tools.showNormalDialogOne(activity, "修改手机", "确认修改？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        businessDao.UpdateUserPhone(MainBActivity.User.getUserId(),
                                "2",
                                MainBActivity.User.getPhone(),
                                etPhone.getText().toString().trim(),
                                etVerification.getText().toString().trim());
                    }
                }, null);

                break;
        }
    }


}
